//#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
//using namespace std;


//void ReverseStr(char* str)
//{
//  int len = strlen(str);
//  char tmp = *str;//保存第一个字符
//  *str = *(str + len - 1);
//  *(str+len-1) = '\0';
//  if(strlen(str +1 ) >= 2)
//    ReverseStr(str+1);
//
//  *(str+len-1) = tmp;
//}
//int main()
//{
//  char str[] = "hello world";
//  ReverseStr(str);
//  printf("%s\n",str);
//  return 0;
//}
//

/* 1.使用assert防止空指针
 * 2.对于不修改的内容,使用const修饰
 * 3.保存字符串的起始地址,方便后序返回
 * 4.保证目标字符串有能力保存要拷贝/追加的内容
 * 5.源字符串要以\0结尾
*/

char* my_strcpy(char* dest,const char* src)
{
  assert(dest && src); 
  char* ret = dest;
  while(*dest++ = *src++) ;
  return ret;
}

char* my_strcat(char* dest,const char* src)
{
  assert(dest && src);
  char* ret = dest;
  while(*dest) dest++;
  while(*dest++ = *src++);
  printf("%s\n",ret);
  return ret;
}
size_t my_strlen(const char* str)
{
  assert(str);
  //size_t count = 0; 
  //while(*str)
  //{
  //  count++;
  //  str++;
  //}
  //return count;
  
  //if(*str) return 1 + my_strlen(str+1);
  //else return 0;
  
  const char* end = str;
  while(*end) end++;
  return end - str;
}
int my_strcmp(const char* s1,const char* s2)
{
  assert(s1 && s2);
  //while(*s1 == *s2)
  //{
  //  if(*s1 == '\0') return 0;
  //  s1++,s2++;
  //}
  //return *s1 - *s2;
  
  while(*s1 && *s2 && (*s1 == *s2))
  {
    s1++,s2++;
  }
  return *s1 - *s2;
}
//s1:主串 s2:子串
char* my_strstr(const char* s1,const char* s2)
{
  assert(s1&&s2);
  if(*s2 == '\0') return s1;
  char* p1 = s1; 
  char* p2 = s2;
  char* cp = s1;
  while(*cp)
  {
    p1 = cp;
    p2 = s2;
    while((*p1 == *p2)&&*p1 && *p2)
    {
        p1++,p2++;
    }
    if(*p2 == '\0')
      return cp;
    else 
      cp++;
  }
  return NULL;
}


char* my_strncpy(char* dest,const char* src,size_t count)
{
  assert(dest && src);
  char* res = dest;
  while(count-- && (*dest++ = *src++) ) ;
  while(count > 0)
  {
    count--;
    *dest++ = '\0';
  }
  return res;
}
int my_strncmp(const char* s1,const char* s2,size_t count)
{
  assert(s1 && s2);
  while(count-- && *s1 && *s2 && (*s1 == *s2)) ;
  return *s1 - *s2;
}
char* my_strncat(char* dest,const char* src,size_t count)
{
  assert(dest && src);
  char* ret = dest;
  while(*dest) dest++;
  while(count-- && (*dest++ = *src++));
  *dest = '\0';
  return ret;
}

void* my_memcpy(void* dest,const void* src,size_t count)
{
  assert(dest && src);
  void* ret = dest;
  while(count--)
  {
    *(char*)dest = *(char*)src;
    dest = (char*)dest+1;
    src = (char*)src+1;
  }
  return ret;
}
int my_memcmp(const void* m1,const void* m2,size_t count)
{
  assert(m1 && m2);
  while(count--)
  {
    if(*(char*)m1 == *(char*)m2)
    {
      m1 = (char*)m1+1;
      m2 = (char*)m2+1;
    }
    else 
      return *(char*)m1 - *(char*)m2 > 0 ? 1:-1;
  }
  return 0;
}
void* my_memmove(void* dest,const void* src,size_t count)
{
  assert(dest && src);
  void* ret = dest;
  //处理内存重叠的问题
  if(dest < src) //从前往后拷贝
  {
    while(count--)
    {
      *(char*)dest = *(char*)src;
      dest = (char*)dest+1;
      src = (char*)src+1;
    }
  }
  else  //从后往前拷贝
  {
    while(count--)
      *((char*)dest + count) = *((char*)src+count);
  }
  return ret;
}
void* my_memset(void* dest,int c,size_t count)
{
  assert(dest);
  void* ret = dest;
  while(count--)
  {
    *(char*)dest = (char)c;
    dest = (char*)dest+1;
  }
  return ret;
}

void* my_memset(void* dest,char c,size_t count)
{
  assert(dest);
  void* ret = dest;
  while(count--)
  {
    *(char*)dest = c - '0';
    dest = (char*)dest+1;
  }
  return ret;
}
int main()
{
  char s[20] = "Mango ";
  const char* str = "hello world";
  
  printf("%s\n",my_strcat(s,str));
  return 0;
}
